package exabase.server;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ServerThread extends Thread {
	private static Logger logger = Logger.getLogger(ServerThread.class
			.getName());
	private Socket socket = null;

	public ServerThread(Socket socket) {
		super("ServerThread");
		this.socket = socket;
	}

	public void run() {
		DataOutputStream os = null;
		BufferedReader is = null;

		try {
			os = new DataOutputStream(socket.getOutputStream());
			is = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));

			os.writeBytes("Hello from server!\n");

			String responseLine;
			while ((responseLine = is.readLine()) != null) {
				System.out.println("Server: " + responseLine);
				os.writeBytes("Ok\n");
				os.flush();
				if (responseLine.indexOf("Ok") != -1) {
					break;
				}
			}
		} catch (IOException e) {
			logger.log(Level.WARNING, "Exception", e);
		}
	}
}